package controller.login;

import domain.entity.UserInfos;
import domain.entity.UserWatchLogin;
import domain.service.user.UserInfosService;
import domain.service.user.UserWatchLoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import util.Base64Util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by bowen on 2016/7/28.
 */
@Controller
public class UserLoginController {

    private static final Logger logger = LoggerFactory.getLogger(UserLoginController.class);

    @Autowired
    private UserInfosService userInfosService;
    @Autowired
    private UserWatchLoginService userWatchLoginService;


    @RequestMapping("userLogin")
    @ResponseBody
    public Map<String,Object> userLogin(String userName,String pwd,String openid) throws UnsupportedEncodingException {
        System.out.println(userName+"-----------------");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        UserInfos userInfo = new UserInfos();
        userInfo.setUSERNAME(userName);
        userInfo.setPWD(Base64Util.encodeBase64(pwd));
        System.out.println(userInfo.getUSERNAME()+"-----------------"+userInfo.getPWD());
        Long cou = userInfosService.getUserInfo(userInfo);
        if(cou > 0 ){
            resultMap.put("success",true);
            UserWatchLogin userWatchLogin = new UserWatchLogin();
            userWatchLogin.setOPENID(openid);
            userWatchLogin.setUSERNAME(userName);
            userWatchLoginService.insertUserWatch(userWatchLogin);
        }else{
            resultMap.put("success",false);
        }
        return resultMap;
    }
}
